/*
 * @Author: Jiraiya
 * @Date: 2020-04-27 15:46:56
 * @LastEditors: Jiraiya
 * @LastEditTime: 2020-05-09 19:41:02
 * @Description: 
 */
const moment = require('moment');
const {logger, mysqlConnect} = global.bee;
const mysql = mysqlConnect(bee.getSystemConfig('mysql'));

function deal(results, fundList = ['110013', '000220']){
    const obj = {}, dates = [], out = {};
    fundList.forEach(e => {
        out[e] = []
    })
    results.forEach((item) => {
        const {FSRQ, code} = item;
        const date = moment(Number(FSRQ)).format('YYYY-MM-DD');
        item.date = date;
        if(obj[date]){
            obj[date][code] = item;
        }else{
            dates.push(date);
            obj[date] = {};
            obj[date][code] = item;
        }
    })

    dates.forEach((date) => {
        const current = obj[date];
        fundList.forEach(e => {
            out[e].push(current[e] || '-');
        })
    })
    
    return out;
    
}

export default async (req, res) => {
    
    // const {code} = req.body;
    // const codeArr = code.split(',');

    const {results = []} = await mysql.query(`select * from fund_value where code = '110013' OR code = '000220' ORDER BY FSRQ asc`);
    
    res.setHeader('Content-Type', 'application/json; charset=utf-8');
    res.end(JSON.stringify(deal(results)));
}